package com.nineclock.message.listener;

import com.alibaba.fastjson.JSON;
import com.nineclock.message.pojo.NcMessage;
import com.nineclock.message.service.MessageService;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;


@Component
@RocketMQMessageListener(topic = "signMessagePushTopic", consumerGroup = "signConsumerGroup")
@Slf4j
public class MsgSignConsumerListener implements RocketMQListener<String> {


    @Autowired
    private MessageService messageService;

    @Override
    public void onMessage(String message) {
        log.info("收到消息，消息内容：" + message);

        try {
            // 将消息内容保存到数据库，调用极光的api推送消息
            NcMessage ncMessage = JSON.parseObject(message, NcMessage.class);

            messageService.pushMessage(ncMessage);

        } catch (Exception e) {
            log.error("收到消息，发生异常：", e);
        }

    }
}
